# -*- coding: utf-8 -*-

from odoo import fields, api
from odoo_contrib import models as contrib_models
from odoo import tools


class Carousel(contrib_models.SchemaModel):
    _name = 'exhibition.carousel'
    _description = 'Carousel for exhibition APP'
    _order = 'create_date desc'

    name = fields.Char(string='标题', compute='_compute_name', store=True)
    image = fields.Binary(string='轮播图', attachment=True)
    image_thumbnail = fields.Binary(string='轮播图', attachment=True)
    is_video = fields.Boolean(string='是否为视频', default=False)
    enabled = fields.Boolean(string='是否启用', default=True)

    article_id = fields.Many2one(
        'exhibition.article',
        string='关联文章',
        domain='[("enabled", "=", True)]',
    )
    article_title = fields.Char(string='文章标题', related='article_id.title')

    exhibit_id = fields.Many2one(
        'exhibition.exhibit',
        string='关联展品',
        domain='[("enabled", "=", True)]',
    )
    exhibit_name = fields.Char(string='展品名称', related='exhibit_id.name')

    editor_id = fields.Many2one(
        'exhibition.users',
        string='轮播图编辑',
    )
    editor_name = fields.Char(string='编辑', related='editor_id.name')
    editor_res_user_id = fields.Many2one(
        'res.users',
    )

    image_id = fields.Integer(compute='_compute_image_id')

    @api.one
    @api.depends('image')
    def _compute_image_id(self):
        attachment = self.env['ir.attachment'].search([
            ('res_model', '=', 'exhibition.carousel'),
            ('res_field', '=', 'image'),
            ('res_id', '=', self.id)
        ], limit=1)
        self.image_id = attachment.id

    @api.multi
    @api.depends('article_id', 'exhibit_id')
    def _compute_name(self):
        for carousel in self:
            if carousel.exhibit_id:
                carousel.name = carousel.exhibit_id.name
            elif carousel.article_id:
                carousel.name = carousel.article_id.title
            else:
                carousel.name = carousel.id

    @api.multi
    def write(self, vals):
        if 'image' in vals:
            images = tools.image_get_resized_images(
                vals['image'],
                return_small=False,
                medium_name='image_thumbnail'
            )
            vals.update(images)
        return super().write(vals)

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            vals['editor_res_user_id'] = self.env.uid
            exhibition_user = self.env['exhibition.users'].search([
                ('res_user_id', '=', self.env.uid)
            ], limit=1)
            vals['editor_id'] = exhibition_user.id
            images = tools.image_get_resized_images(
                vals['image'],
                return_small=False,
                medium_name='image_thumbnail'
            )
            vals.update(images)
        return super().create(vals_list)

    @api.multi
    def unlink(self):
        for carousel in self:
            carousel.enabled = False
        return True

    @api.multi
    def toggle_enabled(self):
        for carousel in self:
            carousel.enabled = not carousel.enabled

    @api.multi
    def build_image_thumbnail(self):
        self = self.sudo()
        for carousel in self:
            if carousel.image:
                images = tools.image_get_resized_images(
                    carousel.image,
                    return_small=False,
                    medium_name='image_thumbnail'
                )
                carousel.image_thumbnail = images['image_thumbnail']

